<!----------游戏页面--------->
<template>
  <div class="game-wraper">
  	<button class="btn btn-smallersize btn-bluebg btn-gotorecharge" v-on:click="gotoRecharge">充值</button>
    <div v-if="flags" class="floatlogo" id="moveDiv" @click="go"
         @mousedown="down" @touchstart="down"
         @mousemove="move" @touchmove="move"
         @mouseup="end" @touchend="end">
      <img src="static/img/icon-float.png">
    </div>
  </div>
</template>

<script>
	import Vue from "vue"
  import apiConfig from '../config/api.js'
	import Message from "iview/src/components/message"

	Vue.prototype.$Message = Message
  export default {
    name: 'index',
    data() {
      return {
        flags: true,
        position: {
          x: 0,
          y: 0
        },
        nx: '',
        ny: '',
        dx: '',
        dy: '',
        x: '',
        y: '',
      }
    },
    created() {
      // 判断localStorage 是否有用户信息
      let userInfo = userInfoKit.getUserInfo();
      if (userInfo == null) {
        //用户重来没有登录过
        this.ykLogin();
      } else {
        let token = userInfo.token
        if (token == null || token == "") {
          //userInfo 不存在token
          this.ykLogin();
        } else {
          //去后台验证token是否过期了
          let params = {token: token, appid: userInfoKit.appid}
          this.$http({method: "GET", url: apiConfig.app.user.verifyToken, params: params, emulateJSON: true})
            .then(function (data) {
              if (data.body != null && data.body.code === "200") {
                console.log("token有效")
              } else {
                console.log("token验证接口返回数据: " + JSON.stringify(data.body))
                this.ykLogin();
              }
            }, function (resp) {
              console.log("token验证接口调用失败")
              this.ykLogin();
            })
        }
      }
    },
    methods: {
      // 实现移动端拖拽
      down: function () {
        var touch;
        if (event.touches) {
          touch = event.touches[0];
        } else {
          touch = event;
        }
        this.position.x = touch.clientX;
        this.position.y = touch.clientY;
        this.dx = moveDiv.offsetLeft;
        this.dy = moveDiv.offsetTop;
      },
      move: function () {
        if (this.flags) {
          var touch;
          if (event.touches) {
            touch = event.touches[0];
          } else {
            touch = event;
          }
          this.nx = touch.clientX - this.position.x;
          this.ny = touch.clientY - this.position.y;
          this.x = this.dx + this.nx;
          this.y = this.dy + this.ny;
          moveDiv.style.left = this.x + "px";
          moveDiv.style.top = this.y + "px";
          if (this.x < 0) {
            moveDiv.style.left = '0px';
          } else if (this.x > (document.documentElement.clientWidth - 50)) {
            moveDiv.style.left = (document.documentElement.clientWidth - 50) + 'px';
          }
          if (this.y < 0) {
            moveDiv.style.top = '0px';
          } else if (this.y > (document.documentElement.clientHeight - 50)) {
            moveDiv.style.top = (document.documentElement.clientHeight - 50) + 'px';
          }
          //阻止页面的滑动默认事件
//           document.addEventListener("touchmove", function () {
//             event.preventDefault();
//           }, false);
        }
      },
      //鼠标释放时候的函数
      end: function () {
        // this.flags = false
      },
      go: function () {
        // 如果是登录状态并且已经绑定了手机号
        if (userInfoKit.isLogin() && userInfoKit.getUserInfo().mobile !== "") {
          this.$router.push("/userswitch")
        } else {
          this.$router.push("/userbind")
        }
      },
      ykLogin : function () {
        console.log("调用游客登录接口")
        let params = {deviceId: userInfoKit.getDeviceId(), appid: userInfoKit.appid}
        this.$http({method: "GET", url: apiConfig.app.user.ykLogin, params: params, emulateJSON: true})
          .then(function (data) {
            if (data.body != null && data.body.code === "200") {
              // 保存到localStorage
              userInfoKit.save(data.body)
            } else {
              console.log("游客登录接口返回数据: " + JSON.stringify(data.body))
            }
          }, function (resp) {
            console.log("游客登录接口调用失败")
          })
      },
      gotoRecharge: function(){
//      // 如果是游客登录,则跳转到登录页面
//      if (userInfoKit.isTourist()) {
//        let _this = this;
//        this.$Message.warning("请先登录")
//        setTimeout(function () {
//          _this.$router.push("/userbind")
//        },1000)
//        return
//      }
  			//请求参数
				let params = {
		        appid: userInfoKit.appid,
		        token: userInfoKit.getUserInfo().token,
		        productId:"100000012",
		        mchOrderId:"201903222026",
		        productName:"笑傲江湖游戏货币",
		        amount:"1.00",
		        roleName:"张无忌",
		        extra:"201903222026",
		        serverId:1001,	//区服ID
		        gameUrl:"https://www.baidu.com",
		        isDev:0,
		        //sign:"5f75cc927f66e9727d7536211f3ea03c"
		    }
		    // 请求收银台接口
		    this.$http(
	    	{
	           method: "GET",
	           url: apiConfig.app.user.getPayUrl,
	           params: params,
	           emulateJSON: true
	    	}).then(function (resp) {
		    	if (resp.body.code == "200") {
		        	// 成功后跳转到收银台页面
		        	this.$router.push({name: "recharge", params: {
		        				productName: resp.body.data.productName,
		        				amount: resp.body.data.amount,
		        				alipay: resp.body.data.alipay
		        			}
		        	});
		    } else if(resp.body.code == "401") {         
		         this.showPop()
		    	}else{	    		
						this.$Message.warning(resp.body.msg)
		    	}
	    	})

      },
      showPop: function () {
        this.$messagebox({
          title: '',
          message: '您尚未实名认证，根据国家网游实名制要求，无法进行充值，是否前往实名认证？',
          showCancelButton: true,
          confirmButtonText: "前往认证",
          cancelButtonText: "取消"
        }).then(action => {
          if (action == 'confirm') {
            console.log('前往认证')
            //跳转到实名认证页面
            this.$router.push("/certify")
          } else {
            console.log('取消')
          }
        })

      }
    }
  }

//备用orderId,extra,sign

//24: {orderId: "201903222024", extra: "201903222024", sign: "ee01a64623fc3ddce4d3123efedf54d9"}
//25: {orderId: "201903222025", extra: "201903222025", sign: "eb183096417940f8d2c43e625263c1b7"}
//26: {orderId: "201903222026", extra: "201903222026", sign: "5f75cc927f66e9727d7536211f3ea03c"}
//27: {orderId: "201903222027", extra: "201903222027", sign: "014f6d7fe38e33a8fc1d1ee1b0fde9f2"}
//28: {orderId: "201903222028", extra: "201903222028", sign: "cdb56422cb57f1f91e18742fdf497e75"}
//29: {orderId: "201903222029", extra: "201903222029", sign: "07dd3d999e406139891ae30b8d454f64"}
//30: {orderId: "201903222030", extra: "201903222030", sign: "aee9455fd2890aa0584f430c9eb6756f"}
//31: {orderId: "201903222031", extra: "201903222031", sign: "83ce6f06fa334758e63372d3623167ae"}
//32: {orderId: "201903222032", extra: "201903222032", sign: "1724b65225ddbcd3b25cb2bdc9715d98"}
//33: {orderId: "201903222033", extra: "201903222033", sign: "266064387424318b08510a379ff3f10e"}
//34: {orderId: "201903222034", extra: "201903222034", sign: "daea8cd6d86756454a4f29c0ae952270"}
//35: {orderId: "201903222035", extra: "201903222035", sign: "14d93d6cbcc1cc5a1fd9f3d61271fee0"}
//36: {orderId: "201903222036", extra: "201903222036", sign: "0b7fa80f64ba3b2a9e8877e47d08b620"}
//37: {orderId: "201903222037", extra: "201903222037", sign: "3524a88b498798fd3049655ad7b4a9c8"}
//38: {orderId: "201903222038", extra: "201903222038", sign: "d0431702c1985af24bd6edeff9217c39"}
//39: {orderId: "201903222039", extra: "201903222039", sign: "9568d4d57b788d45b0161fda062d7deb"}
//40: {orderId: "201903222040", extra: "201903222040", sign: "318968771d46377e3742ccdea76c850e"}
//41: {orderId: "201903222041", extra: "201903222041", sign: "c8f06c32af5e2a7f5f9006e04e49f227"}
//42: {orderId: "201903222042", extra: "201903222042", sign: "a135fda1b281abf6ae905e80cf79e716"}
//43: {orderId: "201903222043", extra: "201903222043", sign: "37350767e97782a78674318907501009"}
//44: {orderId: "201903222044", extra: "201903222044", sign: "169e6f8a3499159a9a2627c812cab166"}
//45: {orderId: "201903222045", extra: "201903222045", sign: "8ad262b81a0693706b710e9dfd36eb97"}
//46: {orderId: "201903222046", extra: "201903222046", sign: "23b2672b862bc44c8c9807d02a1cff21"}
//47: {orderId: "201903222047", extra: "201903222047", sign: "88e0b57155bb0e2ba13c5a6db3c8a253"}
//48: {orderId: "201903222048", extra: "201903222048", sign: "8541713cfb48ec3a03013586ac493f47"}
//49: {orderId: "201903222049", extra: "201903222049", sign: "36f5700e2a725ec68e3214c82fe02353"}










</script>
